<template>
  <div>
    <!-- 表单 -->
    <el-form ref="rulesForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <!-- <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择" value="10" />
        <el-option /> -->
        <el-select v-model="formData.formOfEmployment" placeholder="请选择" style="width:50%">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select></el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="onfocus" />
        <div v-if="showTree" class="tree-box">
          <el-tree :data="departmentList" :props="{ label: 'name' }" @node-click="itemClick" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
      <el-form-item>
        <el-button @click="cansol">取消</el-button>
        <el-button type="primary" @click="onSub">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addEmployee } from '@/api/employees'
import { getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'

export default {
  data() {
    return {
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      options: [{
        value: '1',
        label: '正式'
      }, {
        value: '2',
        label: '非正式'
      }],
      departmentList: [],
      showTree: false,
      value: '',
      rules: {
        username: [{ required: true, message: '此项为必填项', trigger: 'blur' }], // 姓名
        mobile: [{ required: true, message: '此项为必填项', trigger: 'blur' }], // 手机号
        formOfEmployment: 1, // 聘用形式
        workNumber: [{ required: true, message: '此项为必填项', trigger: 'blur' }], // 工号
        departmentName: [{ required: true, message: '此项为必填项', trigger: 'blur' }], // 组织名称
        timeOfEntry: [{ required: true, message: '此项为必填项', trigger: 'blur' }], // 入职时间
        correctionTime: [{ required: true, message: '此项为必填项', trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  mounted() {
    this.getDepartList()
  },
  methods: {
    // 获取部门信息
    async getDepartList() {
      const { data } = await getDepartmentList()
      console.log(data)
      const tree = tranListToTreeData(data.data.depts)
      console.log('部门 信息', tree)
      this.departmentList = tree
    },
    itemClick(item) {
      console.log(item)
      if (item.children.length === 0) {
        this.formData.departmentName = item.name
        this.showTree = false
      }
    },
    onSub() {
      this.$refs.rulesForm.validate(async val => {
        if (val) {
          try {
            const res = await addEmployee(this.formData)
            console.log(res)
            this.$refs.rulesForm.resetFields()
          } catch (error) {
            alert(error)
          }

          this.$emit('submit')
        }
      })
    },
    onfocus() {
      this.showTree = true
    },
    cansol() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
